<?php
/**
 * @author 教书先生
 * @link https://blog.oioweb.cn
 * @date 2020年11月20日14:52:28
 */
header('Content-type:application/json; charset=utf-8');
header('Access-Control-Allow-Origin: *');
$redis = new Redis();
$redis->connect('127.0.0.1');
isset($_REQUEST['url']) ? $data = $_REQUEST : exit('{"code":"-1","msg":"参数不完整"}');

if ($data['switch'] == "on") {
    $url = $data['url'];
    $pwd = $data['pwd'];
    if (filter_var($url, FILTER_VALIDATE_URL) !== false) {
        $html = get_curl($url);
        $url = "https://" . parse_url($url)['host'];
        $json = json_decode(get_curl($url . "/ajaxm.php", [
            "post" => GetBetween($html, "data : '", "'+pwd,") . $pwd,
            'refer' => $url
        ]));
        if ($json->zt === 1) {
            tj($data['url']);
            echo '{"code":1,"msg":"解析成功","url":"' . $json->dom . '/file/' . $json->url . '"}';
        } else {
            echo '{"code":-1,"msg":"' . $json->inf . '"}';
        }
    } else {
        echo '{"code":-1,"msg":"url 地址不正确"}';
    }
} else {
    $url = $data['url'];
    if (filter_var($url, FILTER_VALIDATE_URL) !== false) {
        preg_match_all('/<iframe[^>]*\s+src="([^"]*)"[^>]*>/is', get_curl($url), $matched);
        $url = "https://" . parse_url($url)['host'];
        $refer = get_curl($url . $matched[1][1]);
        $sign = GetBetween($refer, "ajaxdata = '", "';");
        $json = json_decode(get_curl($url . "/ajaxm.php", [
            "post" => [
                "action" => 'downprocess',
                'sign' => $sign,
                'ves' => 1
            ],
            'refer' => $url
        ]));
        if ($json->zt === 1) {
            tj($data['url']);
            echo '{"code":1,"msg":"解析成功","url":"' . $json->dom . '/file/' . $json->url . '"}';
        } else {
            echo '{"code":-1,"msg":"' . $json->inf . '"}';
        }
    } else {
        echo '{"code":-1,"msg":"url 地址不正确"}';
    }
}


function tj($url)
{
    global $redis;
    if ($redis->exists("lanzou_msg")){
        $data = json_decode($redis->get("lanzou_msg"));
        foreach ($data as $value) {
            if ($value->url === $url) {
                return false;
            }
        }
        $data[]=[
            'url' => $url,
            'ip' => real_ip(2),
            'time' => date('Y-m-d H:i:s'),
            'terminal' => terminal()
        ];
        $redis->set("lanzou_msg", json_encode($data, 320), 60 * 60 * 24 * 7);
    }else{
        $json[] = [
            'url' => $url,
            'ip' => real_ip(2),
            'time' => date('Y-m-d H:i:s'),
            'terminal' => terminal()
        ];
        $redis->set("lanzou_msg", json_encode($json, 320), 60 * 60 * 24 * 7);
    }
    return true;
}


function terminal()
{
    if (stripos($_SERVER['HTTP_USER_AGENT'], 'iPhone') || stripos($_SERVER['HTTP_USER_AGENT'], 'iPad')) {
        return "iPhone";
    } else if (stripos($_SERVER['HTTP_USER_AGENT'], 'Android')) {
        return "Android";
    } else {
        return "Windows";
    }
}

function real_ip($type = 2)
{
    $ip = $_SERVER['REMOTE_ADDR'];
    if ($type <= 0 && isset($_SERVER['HTTP_X_FORWARDED_FOR']) && preg_match_all('#\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}#s', $_SERVER['HTTP_X_FORWARDED_FOR'], $matches)) {
        foreach ($matches[0] as $xip) {
            if (filter_var($xip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)) {
                $ip = $xip;
                break;
            }
        }
    } elseif ($type <= 0 && isset($_SERVER['HTTP_CLIENT_IP']) && filter_var($_SERVER['HTTP_CLIENT_IP'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)) {
        $ip = $_SERVER['HTTP_CLIENT_IP'];
    } elseif ($type <= 1 && isset($_SERVER['HTTP_CF_CONNECTING_IP']) && filter_var($_SERVER['HTTP_CF_CONNECTING_IP'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)) {
        $ip = $_SERVER['HTTP_CF_CONNECTING_IP'];
    } elseif ($type <= 1 && isset($_SERVER['HTTP_X_REAL_IP']) && filter_var($_SERVER['HTTP_X_REAL_IP'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)) {
        $ip = $_SERVER['HTTP_X_REAL_IP'];
    }
    return $ip;
}

function get_curl($url, $paras = array())
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    if (@$paras['Header']) {
        $Header = $paras['Header'];
    } else {
        $Header[] = "Accept:*/*";
        $Header[] = "Accept-Encoding:gzip,deflate,sdch";
        $Header[] = "Accept-Language:zh-CN,zh;q=0.8";
        $Header[] = "Connection:close";
    }
    curl_setopt($ch, CURLOPT_HTTPHEADER, $Header);
    if (@$paras['ctime']) { // 连接超时
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $paras['ctime']);
    } else {
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
    }
    if (@$paras['rtime']) { // 读取超时
        curl_setopt($ch, CURLOPT_TIMEOUT, $paras['rtime']);
    }
    if (@$paras['post']) {
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $paras['post']);
    }
    if (@$paras['header']) {
        curl_setopt($ch, CURLOPT_HEADER, true);
    }
    if (@$paras['cookie']) {
        curl_setopt($ch, CURLOPT_COOKIE, $paras['cookie']);
    }
    if (@$paras['refer']) {
        if ($paras['refer'] == 1) {
            curl_setopt($ch, CURLOPT_REFERER, 'http://m.qzone.com/infocenter?g_f=');
        } else {
            curl_setopt($ch, CURLOPT_REFERER, $paras['refer']);
        }
    }
    if (@$paras['ua']) {
        curl_setopt($ch, CURLOPT_USERAGENT, $paras['ua']);
    } else {
        curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36");
    }
    if (@$paras['nobody']) {
        curl_setopt($ch, CURLOPT_NOBODY, 1);
    }
    curl_setopt($ch, CURLOPT_ENCODING, "gzip");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    if (@$paras['GetCookie']) {
        curl_setopt($ch, CURLOPT_HEADER, 1);
        $result = curl_exec($ch);
        preg_match_all("/Set-Cookie: (.*?);/m", $result, $matches);
        $headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
        $header = substr($result, 0, $headerSize); //状态码
        $body = substr($result, $headerSize); //响应内容
        $ret = [
            "Cookie" => $matches, "body" => $body, "code" => $header
        ];
        curl_close($ch);
        return $ret;
    }
    $ret = curl_exec($ch);
    if (@$paras['loadurl']) {
        $Headers = curl_getinfo($ch);
        $ret = $Headers['redirect_url'];
    }
    curl_close($ch);
    return $ret;
}

function GetBetween($content, $start, $end)
{
    $r = explode($start, $content);
    if (isset($r[1])) {
        $r = explode($end, $r[1]);
        return $r[0];
    }
    return '';
}